package com.fqh.controller;

import com.fqh.entity.Admin;
import com.fqh.entity.JsonResult;
import com.fqh.entity.Login;
import com.fqh.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("/login.do")
    public JsonResult login(HttpServletRequest request, String username, String password) {
        JsonResult result = null;
        if ("admin".equals(username) && "admin".equals(password)) {
            result = new JsonResult("登录成功", true);
            Admin admin = new Admin();
            admin.setAdminName(username);
            admin.setAdminPassword(password);
            request.getSession().setAttribute("admin", admin);
        } else {
            result = new JsonResult("用户名或密码错误", false);
        }
        return result;
    }

    @RequestMapping("findAllUser.do")
    public List<Login> findAllUser() {
        return adminService.findAllUser();
    }

    @RequestMapping("logout.do")
    public Boolean logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return true;
    }
}
